package com.itheima.comtroller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.service.AddressService;
import com.itheima.service.OrderService_lzl;
import com.itheima.service.SetmealService_lzl;
import com.itheima.utils.AES;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/order_lzl")
public class OrderController_lzl {
    @Reference
    private OrderService_lzl orderService_lzl;
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService_lzl setmealService_lzl;
    @Reference
    private AddressService addressService;

    /*
     * 提交预约，判断验证码是否正确
     * */
    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody Map map) {
        String code = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String validateCode = jedisPool.getResource().get( RedisMessageConstant.SENDTYPE_ORDER+telephone );
        System.out.println("验证码" + validateCode);
        if (code != null && validateCode != null && code.equals(validateCode)) {
            //验证成功
            Result result = null;
            try {
                result = orderService_lzl.submitOrder(map);


            } catch (Exception e) {
                return result;
            }
            if (result.isFlag()) {
                try {

                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        } else {
            //验证码错误
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    /*
     * 根据orderid，查询预约信息
     * */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService_lzl.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /*
     * 根据条件分页查询
     * */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody  Map queryStrings,HttpServletRequest request) throws Exception {
        Member member = new Member();
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("login_telephone".equals(name)) {
                    String value = cookie.getValue();
                    //使用AES进行解密
                    String decrypt = AES.aesDecrypt(value, AES.KEY);
                    String s = jedisPool.getResource().get(decrypt);
                    member = JSON.parseObject(s, Member.class);
                }
            }
        }
        PageResult pageResult = orderService_lzl.findPage(queryStrings,member);
        return pageResult;
    }
    /*
     * 根据orderid，查询预约信息
     * */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Map map = orderService_lzl.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    /*
     * 根据id，查询setmeal对象，并进行返回
     * */
    @RequestMapping("/findSetmealByid")
    public Result findSetmealByid(Integer id){
        try {
            Setmeal setmeal = setmealService_lzl.findByid(id);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmeal);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }

    }

    /*
     * 根据id，查询address对象，并进行返回
     * */
    @RequestMapping("/findAddressByid")
    public Result findAddressByid(Integer id){
        try {
            Address address = addressService.findByid(id);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,address);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }

    }
}